home *** CD-ROM | disk | FTP | other *** search
/ Aminet 3 / Aminet 3 - July 1994.iso / Aminet / dev / lang / J4thDemo.lha / Tutorials / Tutorial < prev   
Encoding:
Text File  |  1992-10-06  |  24.0 KB  |  698 lines

  1.  
  2.                    BEGINNER'S FORTH TUTORIAL
  3.     
  4.     by Phil Burk
  5.     
  6.     
  7.                             Purpose
  8.                             -------
  9.     
  10.     The intent of this tutorial is to provide a series of 
  11.     experiments that will introduce you to the major concepts of 
  12.     Forth.  It is only a starting point.  Feel free to deviate 
  13.     from the sequences I provide.  A free form investigation 
  14.     that is based on your curiosity is probably the best way to 
  15.     learn any language and Forth is especially well adapted to 
  16.     this type of learning.   There are a number of excellent 
  17.     text books on Forth that you may want to use along with this 
  18.     tutorial.
  19.     
  20.     While following the tutorial, when you see ENTER: you should 
  21.     type in the next set of statements right into the JForth 
  22.     window.  You can enter them in upper or lower case.  At the 
  23.     end of each line, press the RETURN (or ENTER) key; this 
  24.     causes JForth to interpret what you've entered.  However, if 
  25.     you don't see ENTER:, then don't type anything in.
  26.     
  27.     Note that statements between parenthesis, ( like this ) OR 
  28.     following a backslash, \ like this, do NOT have to be typed 
  29.     in, as they are Forth-style comments provided for clarity.
  30.     
  31.     This Tutorial is divided into the following sections:
  32.     
  33.        Purpose
  34.        Forth Syntax
  35.        The Stack  
  36.        Arithmetic Operations
  37.        Defining a New Word
  38.        More Arithmetic
  39.        Arithmetic Overflow
  40.        Converting Algebraic Expressions to Forth
  41.        Character Input and Output
  42.        Using the Precompiled Amiga Data Structures and Function Calls
  43.        Opening and Closing Libraries
  44.     
  45.     Several problems are presented in this tutorial; the answers 
  46.     may be found at the end.
  47.     
  48.     
  49.                         Forth Syntax
  50.                         ------------
  51.     
  52.     Forth has one of the simplest syntaxes, or formats, of any 
  53.     computer language.  The syntax can be stated as follows, 
  54.     "Forth code is a bunch of words with spaces between them." 
  55.     This is even simpler than English!
  56.       
  57.     Each word is equivalent to a function or subroutine in a 
  58.     language like 'C'.  They are executed in the order they 
  59.     appear in the code.  The following statement, for example, 
  60.     could appear in a Forth program:
  61.     
  62.     WAKE.UP  EAT.BREAKFAST  WORK  EAT.DINNER  PLAY  SLEEP
  63.     
  64.     Forth word names can have any combination of letters, 
  65.     numbers, or punctuation.  We will encounter words with names 
  66.     like:
  67.     
  68.         ."  #S  SWAP MOVE !  @  .   *
  69.         
  70.     They are all called words.  The word $%%-GL7OP is a legal 
  71.     Forth name, although not a very good one.  It is up to the 
  72.     programmer to name words in a sensible manner.
  73.     
  74.     
  75.                             The Stack      
  76.                             ---------
  77.     
  78.     The Forth language is based on the concept of a stack.  
  79.     Imagine a stack of blocks with numbers on them.  You can add 
  80.     or remove numbers from the top of the stack.  You can also 
  81.     rearrange the order of the numbers.  Forth uses several 
  82.     stacks.  The Parameter Stack is the one used for passing 
  83.     data between Forth words so we will concentrate our 
  84.     attention there.  The Parameter Stack is also known as the 
  85.     Data Stack.  The Return Stack is another Forth stack that is 
  86.     primarily for internal system use.  In this tutorial, when 
  87.     we refer to the "stack," we will be referring to the 
  88.     Parameter Stack.  The stack is initially empty.  To put some 
  89.     numbers on the stack, ENTER:
  90.     
  91.     23 7 9182 
  92.     
  93.     You will notice that the 'ok' message now has a '3' in front 
  94.     of it.  This tells you that there are three numbers on the 
  95.     stack.
  96.        
  97.     Let's now print the number on top of the stack using the 
  98.     Forth word '.' which is pronounced "dot".  This is a hard 
  99.     word to write about in a text-based tutorial because it is a 
  100.     single period.  ENTER:
  101.     
  102.     .
  103.     
  104.     You should see the last number you entered, 9,182 ,  
  105.     printed.  Forth has a very handy word for showing you what's 
  106.     on the stack.  It is .S, which is pronounced "dot S".  The 
  107.     name was constructed from "dot" for print, and "S" for 
  108.     stack.  If you enter:
  109.     
  110.     .S
  111.     
  112.     you will see your numbers in a list.  The number at the far 
  113.     right is the one on top of the stack.
  114.     
  115.     You will notice that the 9182 is not on the stack.  The word 
  116.     '.' removes the number on top of the stack before printing 
  117.     it.  In contrast, '.S' leaves the stack untouched.
  118.       
  119.     We have a way of documenting the effect of words on the 
  120.     stack with a stack diagram.  A stack diagram is contained in 
  121.     parentheses.  In Forth, the parentheses indicate a comment.  
  122.     In the examples that follow, you do not need to type in the 
  123.     comments.  When you are programming, of course, we encourage 
  124.     the use of comments and stack diagrams to make your code 
  125.     more readable.  In Forth, we often use stack diagrams to 
  126.     illustrate the state of the stack.  Do not type these in.  
  127.     The stack diagram for a word like '.' would be:
  128.     
  129.     . ( N -- , print number on top of stack )
  130.     
  131.     The symbols to the left of -- describe the parameters that a 
  132.     word expects to process.  In this example, N stands for any 
  133.     integer number.  To the right of --, up to the comma, is a 
  134.     description of the stack parameters when the word is 
  135.     finished, in this case there are none because 'dot' "eats" 
  136.     the N that was passed in.  (Note that the stack descriptions 
  137.     are not necessary, but they are a great help when learning 
  138.     other peoples programs.)
  139.     
  140.     The text following the comma is an English description of 
  141.     the word.  You will note that after the --, N is gone.  You 
  142.     may be concerned about the fact that there were other 
  143.     numbers on the stack, namely 23 and 7 .  The stack diagram, 
  144.     however, only describes the portion of the stack that is 
  145.     affected by the word.
  146.     
  147.     Between examples, you will probably want to clear the stack.  
  148.     If you enter 0SP, pronounced "zero S P", then the stack will 
  149.     be cleared.  Since the stack is central to Forth, it is 
  150.     important to be able to alter the stack easily.  Let's look 
  151.     at some more words that manipulate the stack.  ENTER:
  152.       
  153.     0SP .S    \ That's a 'zero' 0, not an 'oh' O.
  154.     777 DUP .S
  155.     
  156.     You will notice that there are two copies of 777 on the 
  157.     stack.  The word DUP duplicates the top item on the stack.  
  158.     This is useful when you want to use the number on top of the 
  159.     stack and still have a copy.  The stack diagram for DUP 
  160.     would be:
  161.      
  162.     DUP ( n -- n n , DUPlicate top of stack )
  163.      
  164.     Another useful word, is SWAP. ENTER:
  165.       
  166.     0SP 23 7 .S
  167.     SWAP .S
  168.     SWAP .S
  169.     
  170.     The stack diagram for SWAP would be:
  171.       
  172.     SWAP ( a b -- b a , swap top two items on stack )
  173.      
  174.     Now ENTER:
  175.       
  176.     OVER .S
  177.     OVER .S
  178.     
  179.     The word OVER causes a copy of the second item on the stack 
  180.     to leapfrog over the first. its stack diagram would be:
  181.       
  182.     OVER ( a b -- a b a , copy second item on stack )
  183.     
  184.     Here is another commonly used Forth word:
  185.     
  186.     DROP  ( a -- , remove item from the stack )
  187.     
  188.     Can you guess what we will see if we ENTER:
  189.     
  190.     0SP 11 22 .S
  191.     DROP .S
  192.     
  193.     Another handy word for manipulating the stack is ROT.  
  194.     ENTER:
  195.     
  196.     0SP 11 22 33 44 .S
  197.     ROT .S
  198.      
  199.     The stack diagram for ROT is, therefore:
  200.       
  201.     ROT  ( a b c -- b c a , ROTate third item to top )
  202.       
  203.     You have now learned the more important stack manipulation 
  204.     words.  You will see these in almost every Forth program.  I 
  205.     should caution you that if you see too many stack 
  206.     manipulation words being used in your code then you may want 
  207.     to reexamine and perhaps reorganize your code.  You will 
  208.     often find that you can avoid excessive stack manipulations 
  209.     by using local or global VARIABLES which will be discussed 
  210.     later.  If you want to grab any arbitrary item on the stack, 
  211.     use PICK .  ENTER:
  212.       
  213.     0SP
  214.     14 13 12 11 10
  215.     3 PICK .    ( prints 13 )
  216.     0 PICK .    ( prints 10 )
  217.     4 PICK .
  218.       
  219.     PICK makes a copy of the Nth item on the stack.  The 
  220.     numbering starts with zero, therefore:
  221.       
  222.     0 PICK   is equivalent to DUP
  223.     1 PICK   is equivalent to OVER
  224.      
  225.     PICK  ( ... v3 v2 v1 v0 N -- ... v3 v2 v1 v0 vN )
  226.       
  227.     (Warning. The Forth-79 and FIG Forth standards differ from 
  228.     the Forth '83 standard in that their PICK numbering starts 
  229.     with one, not zero.)
  230.       
  231.     I have included the stack diagrams for some other useful 
  232.     stack manipulation words.  Try experimenting with them by 
  233.     putting numbers on the stack and calling them to get a feel 
  234.     for what they do.  Again, the text in parentheses is just a 
  235.     comment and need not be entered.
  236.     
  237.        DROP  ( n -- , remove top of stack )  
  238.        ?DUP  ( n -- n n | 0 , duplicate only if non-zero, '|' means OR )  
  239.        -ROT  ( a b c -- c a b , rotate top to third position )  
  240.        2SWAP ( a b c d -- c d a b , swap pairs )  
  241.        2OVER ( a b c d -- a b c d a b , leapfrog pair )  
  242.        2DUP  ( a b -- a b a b , duplicate pair )  
  243.        2DROP ( a b -- , remove pair )  
  244.        NIP   ( a b -- b , remove second item from stack )  
  245.        TUCK  ( a b -- b a b , copy top item to third position )  
  246.     
  247.     Problems:
  248.     
  249.     Start each problem by entering:
  250.       
  251.     0SP 11 22 33
  252.      
  253.     Then use the stack manipulation words you have learned to 
  254.     end up with the following numbers on the stack:
  255.       
  256.     1)      11 33 22 22 
  257.     
  258.     2)      22 33 
  259.     
  260.     3)      22 33 11 11 22 
  261.     
  262.     4)      11 33 22 33 11 
  263.     
  264.     5)      33 11 22 11 22 
  265.     
  266.     Answers to the problems can be found at the end of this 
  267.     tutorial.
  268.     
  269.     
  270.                     Arithmetic Operations
  271.                     ---------------------
  272.     
  273.     Great joy can be derived from simply moving numbers around 
  274.     on a stack. Eventually, however, you'll want to do something 
  275.     useful with them.  This section describes how to perform 
  276.     arithmetic operations in Forth.
  277.        
  278.     The Forth arithmetic operators work on the numbers currently 
  279.     on top of the stack.  If you want to add the top two numbers 
  280.     together, use the Forth word + pronounced "plus".  ENTER:
  281.       
  282.     2 3 + .  
  283.     2 3 + 10 + . 
  284.     
  285.     This style of expressing arithmetic operations is called 
  286.     Reverse Polish Notation, or RPN.  It will already be 
  287.     familiar to those of you with HP calculators.  In the 
  288.     following examples, I have put the algebraic equivalent 
  289.     representation in a comment.
  290.     
  291.     Some other arithmetic operators are  - * / . ENTER:
  292.     
  293.     30 5 - .      \ 25=30-5
  294.     30 5 / .      \ 6=30/5
  295.     30 5 * .      \ 150=30*5
  296.     30 5 + 7 / .  \ 5=(30+5)/7
  297.     
  298.     Some combinations of operations are very common and have 
  299.     been coded in assembly language for speed.  For example, 2* 
  300.     is short for 2 * .  You should use these whenever possible 
  301.     to increase the speed of your program. These include:
  302.       
  303.     1+  1-  2+  2-  2*  2/
  304.      
  305.     Try entering:
  306.       
  307.     10 1- .  
  308.     7 2* 1+ .  ( 15=7*2+1 )
  309.     
  310.     One thing that you should be aware of is that when you are 
  311.     doing division with integers using /, the remainder is lost. 
  312.     ENTER:
  313.       
  314.     15 5 / .  
  315.     17 5 / .
  316.       
  317.     This is true in all languages on all computers.  Later we 
  318.     will examine /MOD and MOD which do give the remainder.
  319.     
  320.     
  321.                     Defining a New Word
  322.                     -------------------
  323.     
  324.     It's now time to write a small program in Forth.  You can do 
  325.     this by defining a new word that is a combination of words 
  326.     we have already learned.  Let's define and test a new word 
  327.     that takes the average of two numbers.
  328.        
  329.     We will make use of two new words, : ( "colon"), and ; ( 
  330.     "semicolon") .  These words start and end a typical Forth 
  331.     definition.  When you type this in, you may want to time how 
  332.     long it takes between hitting the RETURN key and the 
  333.     appearance of the OK prompt.  This will be how long it takes 
  334.     Forth to compile and link this small program.  ENTER:
  335.       
  336.     : AVERAGE  ( a b -- avg )  + 2/   ;
  337.      
  338.     Congratulations. You have just written a Forth program.  If 
  339.     you are used to 'C', you were probably wondering what to get 
  340.     from the kitchen while compiling.  One danger of programming 
  341.     in Forth is that, once hooked, you may never take time to 
  342.     eat.
  343.     
  344.     Let's look more closely at what just happened.  The colon 
  345.     told Forth to add a new word to its list of words.  This 
  346.     list is called the Forth dictionary.  The name of the new 
  347.     word will be whatever name follows the colon.  Any Forth 
  348.     words entered after this will be compiled into the new word.  
  349.     This continues until the semicolon is reached which finishes 
  350.     the definition.
  351.     
  352.     Let's test this word, ENTER:
  353.       
  354.     10 20 AVERAGE .  ( should print 15 )
  355.     
  356.     If you wish, you can easily view the assembly code that the 
  357.     JForth compiler created for AVERAGE (note that you do NOT 
  358.     have to understand assembly-language to program in JForth.  
  359.     However, if you do, you will appreciate JForth's extensive 
  360.     Motorola-format support, both in assembly and disassembly.)  
  361.     ENTER:
  362.     
  363.     DEF AVERAGE
  364.     
  365.     Once a word has been defined, it can be used to define more 
  366.     words.  Let's write a word that tests our word AVERAGE.  
  367.     ENTER:
  368.     
  369.     : TEST ( --)  50 60 AVERAGE . ;
  370.     TEST
  371.     
  372.     Try combining some of the words you have learned into new 
  373.     Forth definitions of your choice.  If you promise not to be 
  374.     overwhelmed, you can get a list of the words that are 
  375.     available for programming if you ENTER:
  376.       
  377.     WORDS
  378.      
  379.     You can stop this listing by hitting the SPACE BAR.  To 
  380.     continue, hit the RETURN key.  To stop, ENTER:
  381.     
  382.     QUIT
  383.      
  384.    Don't worry, only a small fraction of these will be used 
  385.    directly in your programs, so you will never have to learn 
  386.    them all.  (Because we've pre-compiled so much in this demo 
  387.    (ARexx, EZGraphics interface, debugger, etc.), there is an 
  388.    'abundance of words' in this dictionary, to put it mildly!)
  389.     
  390.     
  391.                         More Arithmetic
  392.                         ---------------
  393.     
  394.     When you need to know the remainder of a divide operation,
  395.     /MOD will return the remainder as well as the quotient.  the 
  396.     word MOD will only return the remainder.  ENTER:
  397.       
  398.     0SP
  399.     53 10 /MOD .S 
  400.     0SP  
  401.     7 5 MOD .S
  402.     
  403.     Two other handy words are MIN and MAX .  They accept two 
  404.     numbers and return the MINimum or MAXimum value 
  405.     respectively.  ENTER:
  406.       
  407.     56 34 MAX .  
  408.     56 34 MIN .  
  409.     -17 0 MIN .
  410.       
  411.     Some other useful words are:
  412.       
  413.     ABS    ( n -- abs(n) , absolute value of n )  
  414.     NEGATE ( n -- -n , negate value, faster then -1 * )  
  415.     ASHIFT ( n c -- n*(2**c) , arithmetic shift of n )  
  416.     SHIFT  ( n c -- n' , logical shift of n )
  417.       
  418.     ASHIFT can be used if you have to multiply quickly by a 
  419.     power of 2 .  A negative count is like doing a divide.  This 
  420.     is much faster than doing a regular multiply and should be 
  421.     used whenever possible.  ENTER:
  422.       
  423.     : 256*   8 ASHIFT ; 
  424.     3 256* .
  425.     
  426.     
  427.                     Arithmetic Overflow
  428.                     -------------------
  429.     
  430.     If you are having problems with your calculation overflowing 
  431.     the 32-bit precision of the stack, then you can use */ .  
  432.     This produces an intermediate result that is 64 bits long.  
  433.     Try the following three methods of doing the same 
  434.     calculation.  Only the one using */ will yield the correct 
  435.     answer, 5197799.
  436.        
  437.     34867312 99154 * 665134 / .  
  438.     34867312 665134 / 99154 * .  
  439.     34867312 99154 665134 */ .
  440.     
  441.     
  442.             Converting Algebraic Expressions to Forth
  443.             -----------------------------------------
  444.     
  445.     How do we express complex algebraic expressions in Forth?   
  446.     For example:   20 + (3 * 4)
  447.     
  448.     To convert this to Forth you must order the operations in 
  449.     the order of evaluation.  In Forth, therefore, this would 
  450.     look like:
  451.       
  452.     3 4 *  20 +
  453.     
  454.     Evaluation proceeds from left to right in Forth so there is 
  455.     no ambiguity.  Compare the following algebraic expressions 
  456.     and their Forth equivalents: (Do not enter these!)
  457.     
  458.     (100+50)/2   ==>  100 50 +  2/
  459.     ((2*7) + (13*5))  ==>  2 7 *  13 5 *  +
  460.     
  461.     If any of these expressions puzzle you, try entering them 
  462.     one word at a time, while viewing the stack with .S (dot-S).
  463.     
  464.     Problems:
  465.     
  466.     Convert the following algebraic expressions to their 
  467.     equivalent Forth expressions.  (Do not enter these because 
  468.     they are not Forth code!)
  469.     
  470.     (12 * ( 20 - 17 )) 
  471.     (1 - ( 4 * (-18) / 6) )
  472.     ( 6 * 13 ) - ( 4 * 2 * 7 )
  473.      
  474.     Use the words you have learned to write these new words:
  475.       
  476.     SQUARE       ( N -- N*N , calculate square )  
  477.     DIFF.SQUARES ( A B -- A*A-B*B , difference of squares )  
  478.     AVERAGE4     ( A B C D -- [A+B+C+D]/4 )  
  479.     HMS>SECONDS  ( HOURS MINUTES SECONDS -- TOTAL-SECONDS , convert )
  480.     
  481.     
  482.                     Character Input and Output
  483.                     --------------------------
  484.     
  485.     The numbers on top of the stack can represent anything.  The 
  486.     top number might be how many blue whales are left on Earth 
  487.     or your weight in kilograms.  It can also be an ASCII 
  488.     character.  ENTER:
  489.       
  490.     72 EMIT  105 EMIT
  491.      
  492.     You should see the word "Hi" appear before the OK.  The 72 
  493.     is an ASCII 'H' and 105 is an 'i'.  EMIT takes the number on 
  494.     the stack and outputs it as a character.  If you want to 
  495.     find the ASCII value for any character, you can use the word 
  496.     ASCII . ENTER:
  497.      
  498.     ASCII W .  
  499.     ASCII %   DUP .   EMIT 
  500.     ASCII A   DUP .  
  501.     32 + EMIT
  502.      
  503.     Notice that the word ASCII is a bit unusual because its 
  504.     input comes not from the stack, but from the following text.  
  505.     In a stack diagram, we represent that by putting the input 
  506.     in angle brackets, <input>.  Here is the stack diagram for 
  507.     ASCII.
  508.     
  509.     ASCII  ( <char> -- char , get ASCII value of a character )
  510.       
  511.     Using EMIT to output character strings would be very 
  512.     tedious.  Luckily there is a better way.  ENTER:
  513.       
  514.     : TOFU ." Yummy bean curd!" ;
  515.      
  516.     TOFU
  517.      
  518.     The word ." , pronounced "dot quote",  will take everything 
  519.     up to the next quotation mark and print it to the screen.  
  520.     Make sure you leave a space after the first quotation mark.  
  521.     When you want to have text begin on a new line, you can 
  522.     issue a carriage return using the word CR .  ENTER:
  523.       
  524.     : SPROUTS ." Miniature vegetables." ; 
  525.     : MENU 
  526.         CR TOFU CR SPROUTS CR 
  527.     ; 
  528.     MENU
  529.      
  530.     You can emit a blank space with SPACE .  A number of spaces 
  531.     can be output with SPACES .  ENTER:
  532.       
  533.     CR TOFU SPROUTS 
  534.     CR TOFU SPACE SPROUTS 
  535.     CR 10 SPACES TOFU CR 20 SPACES SPROUTS
  536.      
  537.     For character input, Forth uses the word KEY which 
  538.     corresponds to the word EMIT for output.  KEY waits for the 
  539.     user to press a key then leaves its value on the stack.  
  540.     ENTER:
  541.        
  542.     : TESTKEY ( -- ) 
  543.         ." Hit a key: " KEY CR 
  544.         ." That = " . CR 
  545.     ; 
  546.     TESTKEY
  547.     
  548.     The following is a summary of some useful Input/Out words...
  549.      
  550.     EMIT   ( char -- , output character )  
  551.     KEY    ( -- char , input character )  
  552.     SPACE  ( -- , output a space )  
  553.     SPACES ( n -- , output n spaces )  
  554.     ASCII  ( <char> -- char , convert to ASCII )  
  555.     CR     ( -- , start new line , carriage return )  
  556.     ."     ( -- , output " delimited text )
  557.     
  558.       
  559.     Using the Precompiled Amiga Data Structures and Function Calls
  560.     --------------------------------------------------------------
  561.     
  562.     A significant portion of the complete Amiga interface 
  563.     offered in the normal JForth release has been pre-compiled 
  564.     for you in this demo version.  To call an Amiga library 
  565.     function, it is usually only necessary to know the name of 
  566.     that function and possibly pass it the name of an Amiga data 
  567.     structure (such as a Window or Gadget structure).
  568.     
  569.     As an example, let's assume you have stored the 
  570.     JForth-relative address of an Amiga 'Window' structure in a 
  571.     variable called MY-WINDOW.  If the window was open, you 
  572.     could close it with a statement like:
  573.     
  574.         MY-WINDOW @ CLOSEWINDOW()
  575.     
  576.     JForth uses descriptive function names that are identical 
  577.     with those defined by Commodore, and adds the () as a visual 
  578.     cue that this routine is a direct call to the Amiga library 
  579.     system.  Examples of other provided calls include 
  580.     OPENWINDOW(). ISREXXMSG().  CREATESTDIO(), etc.  A complete 
  581.     list can be obtained by typing in:
  582.     
  583.         WORDS-LIKE ()
  584.     
  585.     JForth also provides the ability to define and access all 
  586.     the standard Amiga structures.  Many have been pre-compiled 
  587.     in the demo.  The syntax of a JForth structure reference is:
  588.     
  589.         <structure>  <operator>  <element>
  590.     
  591.     The three available operators are:
  592.     
  593.         S@    \ fetch element contents
  594.         S!    \ store element contents
  595.         ..    \ return address (relative) of element
  596.     
  597.     For example, to read the above window's height...
  598.     
  599.         MY-WINDOW @ s@ wd_Height     ( -- height )
  600.     
  601.     ...to set the same element to 120...
  602.     
  603.         120  MY-WINDOW @ s! wd_Height
  604.     
  605.     ...to get the JForth-relative address of that element...
  606.     
  607.         MY-WINDOW @ .. wd_Height     ( -- address )
  608.     
  609.     Note that the structure names are exactly the same as those
  610.     documented in the ROM KERNEL MANUAL, and the the element
  611.     names are the same as that used in the Amiga's assembler
  612.     files.
  613.     
  614.     
  615.                 Opening and Closing Libraries
  616.                 -----------------------------
  617.     
  618.     As is customary with many languages, you must 'open' a 
  619.     specific Amiga Library before calling its functions.  JForth 
  620.     makes this easy by providing a single open-word for each 
  621.     standard Amiga library.
  622.     
  623.     All you have to do is to simply state the name of the 
  624.     library ending with a '?' character.  For example, to open 
  625.     the 'graphics.library' facility of the Amiga, just enter:
  626.     
  627.         GRAPHICS?
  628.     
  629.     As another example, assume you want to access functions in 
  630.     the 'icon.library', you should, at least once before calling 
  631.     one (it doesn't hurt to do it multiple times), state:
  632.     
  633.         ICON?
  634.     
  635.     See?  It's very easy.  If for some reason the library cannot 
  636.     be opened, JForth will issue a descriptive error message and 
  637.     abort the running program.
  638.     
  639.     Similarly, if you try to call a function in a library that 
  640.     hasn't been opened as above, another error message will 
  641.     appear and the running program will stop executing.
  642.     
  643.     JForth provides a similar simple syntax for closing those 
  644.     libraries that you've opened; it uses a '-' character BEFORE 
  645.     the name.  To close the above-opened libraries, use:
  646.     
  647.         -GRAPHICS
  648.         -ICON
  649.     
  650.     Note that if you forget to close libraries, JForth will do so
  651.     automatically when you exit the JForth program (via BYE).
  652.     
  653.     
  654.     
  655.     
  656.     Answers to Problems:
  657.     
  658.     If your answer doesn't exactly match these but it works, 
  659.     don't fret.  In Forth, there are usually many ways to the 
  660.     same thing.
  661.        
  662.     Stack Manipulations:
  663.       
  664.     1) SWAP DUP 
  665.     2) ROT DROP 
  666.     3) ROT DUP 3 PICK 
  667.     4) SWAP OVER 3 PICK 
  668.     5) -ROT 2DUP
  669.      
  670.     Arithmetic:
  671.       
  672.     (12 * (20 - 17))    ==>   20 17 -  12 * 
  673.     
  674.     (1 - (4 * (-18) / 6))     ==>   1   4 -18 *  6 /  - 
  675.     
  676.     (6 * 13) - (4 * 2 * 7)    ==>   6 13 *  4 2 *  7 *   - 
  677.      
  678.     : SQUARE ( N -- N*N ) 
  679.         DUP * 
  680.     ;
  681.      
  682.     : DIFF.SQUARES ( A B -- A*A-B*B ) 
  683.         SWAP SQUARE 
  684.         SWAP SQUARE - 
  685.     ;
  686.      
  687.     : AVERAGE4 ( A B C D -- [A+B+C+D]/4 ) 
  688.         + + +   ( add'em up ) 
  689.         -2 ashift ( divide by four the fast way, or 4 / ) 
  690.     ;
  691.      
  692.     : HMS>SECONDS  ( HOURS MINUTES SECONDS -- TOTAL-SECONDS ) 
  693.         -ROT SWAP ( -- seconds minutes hours ) 
  694.         60 * +    ( -- seconds total-minutes ) 
  695.         60 * +    ( -- seconds ) 
  696.     ; 
  697.  
  698.